package com.education.platform.api.controller.user;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.constants.RedisCacheConstant;
import com.education.platform.api.service.AccountService;
import com.education.platform.api.service.PointsFlowService;
import com.education.platform.api.service.UserBaseService;
import com.education.platform.api.service.common.CacheDataService;
import com.education.platform.api.vo.request.PointsFlowQueryVO;
import com.education.platform.api.vo.request.UserPointsUpdateVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.redis.RedisUtils;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.common.util.ValidatorUtils;
import com.education.platform.dao.entity.AccountDO;
import com.education.platform.dao.entity.PointsFlowDO;
import com.education.platform.dao.model.PointsFlowModel;
import com.education.platform.dao.model.PointsRankModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import com.education.platform.api.service.UserPointsService;
import org.springframework.validation.annotation.Validated;
import com.education.platform.api.vo.request.UserPointsQueryVO;

/**
* describe: 积分表
* @author : tomato
* @date: 2020-06-06 03:07:32.843
**/
@RestController
@Api("积分")
@RequestMapping("userPoints")
@Validated
public class UserPointsController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(UserPointsController.class);
    
	@Autowired
    private UserPointsService userPointsService;
	@Autowired
    private UserBaseService userBaseService;
	@Autowired
    private PointsFlowService pointsFlowService;
    @Autowired
    private RedisUtils redisUtils;

	@Autowired
    private CacheDataService cacheDataService;

	@Autowired
    private AccountService accountService;


    /**
     * 提交积分
     * @param
     * @return
     */
    @ApiOperation(value = "提交积分")
    @PostMapping("commitPoints")
    public void commitPoints(@RequestBody @Valid UserPointsUpdateVO userPointsUpdateVO) {
        if(!accountService.isVip(ThreadUser.getUserId())){
            throw new BusinessException("您还不是vip用户");
        }
        userPointsService.addPoints( userPointsUpdateVO);
    }


    /**
     * 积分排行榜
     * @param userPointsQueryVO 积分排行榜
     * @return 分页参数
     */
    @ApiOperation(value = "积分排行榜倒叙（可分页）")
    @PostMapping("getRankPageList")
    public UserPointsPageInfoVO getRankPageList(@RequestBody @Valid UserPointsQueryVO userPointsQueryVO) {

        Page<UserPointsPageInfoVO> page = PageHelper.startPage(1, 100);
        List<PointsRankModel> list = userPointsService.getRankList();
        UserPointsPageInfoVO userPointsPageInfo = new UserPointsPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), userPointsPageInfo);
        List<PointsRankVO> voList = BeanUtil.convert(list, PointsRankVO.class);
        PointsRankVO rankVO;
        for(int i=0;i<voList.size();i++){
            rankVO=voList.get(i);
            if(ValidatorUtils.mobile(rankVO.getNickName())){
                rankVO.setNickName( rankVO.getNickName().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
            }
            rankVO.setRank(i+1);
        }
        userPointsPageInfo.setList(voList);
        return userPointsPageInfo;
    }



    /**
     * 获取积分明细
     * @param
     * @return 分页参数
     */
    @ApiOperation(value = "获取积分明细")
    @PostMapping("getPointsFlowList")
    public PointsFlowPageInfoVO getPointsFlowList(@RequestBody @Valid PointsFlowQueryVO queryVO) {
        if(!accountService.isVip(ThreadUser.getUserId())){
            throw new BusinessException("您还不是vip用户");
        }
        String info = String.format("The method name[selectPageList] params:%s", queryVO.toString());
        LOGGER.info(info);
        PointsFlowModel query = BeanUtil.convert(queryVO,PointsFlowModel.class);
        Page<PointsFlowPageInfoVO> page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
        query.setUserId(ThreadUser.getUserId());
        List<PointsFlowDO> list =pointsFlowService .getByCondition(query);

        PointsFlowPageInfoVO pointsPageInfoVO = new PointsFlowPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), pointsPageInfoVO);
        List<PointsFlowResponseVO> voList = BeanUtil.convert(list, PointsFlowResponseVO.class);
        pointsPageInfoVO.setList(voList);
        return pointsPageInfoVO;
    }



    /**
     * 获取当日积分情况
     * @param
     * @return 分页参数
     */
    @ApiOperation(value = "获取当日积分情况")
    @PostMapping("getTodayPoints")
    public UserTodayPointsResponseVO getTodayPoints() {
        if(!accountService.isVip(ThreadUser.getUserId())){
            throw new BusinessException("您还不是vip用户");
        }
        String field = String.valueOf(ThreadUser.getUserId());
        UserTodayPointsResponseVO responseVO  = new UserTodayPointsResponseVO();
        String loginPoint = redisUtils.hgetStr(RedisCacheConstant.HASH_KEY_DAILY_LOGIN,field);
        if(StringUtils.isNotEmpty(loginPoint)){
            responseVO.setSignInPoint(Float.valueOf(loginPoint).intValue());
        }else{
            responseVO.setSignInPoint(0);
        }

        String answerPoint = redisUtils.hgetStr(RedisCacheConstant.HASH_KEY_DAILY_ANSWER,field);
        if(StringUtils.isNotEmpty(answerPoint)){
            responseVO.setAnswerPoint(Float.valueOf(answerPoint));
        }else{
            responseVO.setAnswerPoint(0f);
        }

        String learnVideoPoint = redisUtils.hgetStr(RedisCacheConstant.HASH_KEY_DAILY_LEARN_VIDEO,field);
        if(StringUtils.isNotEmpty(learnVideoPoint)){
            responseVO.setLearnVideoPoint(Float.valueOf(learnVideoPoint));
        }else{
            responseVO.setLearnVideoPoint(0f);
        }

        String learnArticlePoint = redisUtils.hgetStr(RedisCacheConstant.HASH_KEY_DAILY_LEARN_ARTICLE,field);
        if(StringUtils.isNotEmpty(learnArticlePoint)){
            responseVO.setLearnArticlePoint(Float.valueOf(learnArticlePoint));
        }else{
            responseVO.setLearnArticlePoint(0f);
        }

        String sharePoint = redisUtils.hgetStr(RedisCacheConstant.HASH_KEY_DAILY_SHARE,field);
        if(StringUtils.isNotEmpty(sharePoint)){
            responseVO.setSharePoint(Float.valueOf(sharePoint));
        }else{
            responseVO.setSharePoint(0f);
        }
        return responseVO;

    }



}
